#include "StdAfx.h"
#include "VPropEditor.h"

// VProp //////////////////////////////////////////////////////////////////////

//
VProp::VProp()
	:m_object(NULL)
{

}

//
VProp::~VProp()
{

}

// VPropEditor ////////////////////////////////////////////////////////////////

//
VPropEditor::VPropEditor(QWidget *parent)
	:QFrame(parent)
{
	m_layout = new QVBoxLayout;
		m_layout->setContentsMargins(0,0,0,0);
		m_layout->setAlignment(Qt::AlignTop);

	setFrameRect(QRect());
	setLayout(m_layout);
}

//
VPropEditor::~VPropEditor()
{
}

//
void VPropEditor::addProp(VProp *prop)
{
	m_props << VPropPtr(prop);
	
	QLabel *label = new QLabel;
		label->setText(prop->text());
	m_layout->addWidget(label);

	QFormLayout *hLayout = new QFormLayout;
		hLayout->setContentsMargins(0,0,0,0);
		hLayout->addRow("    ",prop->editor());
	m_layout->addLayout(hLayout);
}

///////////////////////////////////////////////////////////////////////////////
// VPropEnum //////////////////////////////////////////////////////////////////

//
VPropEnum::VPropEnum() 
{ 
	m_editor = new QComboBox;

	connect(
		m_editor,SIGNAL(currentIndexChanged(int)),
		this,SLOT(currentIndexChanged(int))
	);
}

//
VPropEnum::~VPropEnum() 
{ 
	delete m_editor;
}

//
void VPropEnum::setValue(int v) 
{ 
	emit valueChanged(v); 
	m_editor->setCurrentIndex(v);
}

// VPropString ////////////////////////////////////////////////////////////////

//
VPropString::VPropString()
{
	m_editor = new QLineEdit;
	
	connect(
		m_editor,SIGNAL( textChanged(const QString &) ),
		this,SLOT( textChanged(const QString &) )
	);
}

//
VPropString::~VPropString()
{
	delete m_editor;
}

//
void VPropString::setValue(const QString &v_) 
{ 
	QString v(v_); 
	emit valueChanged(v); 
	m_editor->setText(v);
	
	if (object()) {
		bool ok = object()->setProperty(name().toAscii().constData(),v);
		Q_ASSERT(ok);
	}
}

// VPropInteger ///////////////////////////////////////////////////////////////

//
VPropInteger::VPropInteger()
{
	m_editor = new QLineEdit;
		m_editor->setValidator(new QIntValidator(m_editor));

	connect(
		m_editor,SIGNAL( textChanged(const QString &) ),
		this,SLOT( textChanged(const QString &) )
	);
}

//
VPropInteger::~VPropInteger()
{
	delete m_editor;
}

//
void VPropInteger::setValue(int v) 
{ 
	emit valueChanged(v); 
	m_editor->setText(QString("%1").arg(v));
}
